package cn.agilean.test.service;

import cn.agilean.test.entity.Product;
import cn.agilean.test.repo.BlackRepo;
import cn.agilean.test.repo.CompanyRepo;
import cn.agilean.test.repo.ProductRepo;
import cn.agilean.test.vo.CompanyVo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class ProductManager {
    @Resource
    private ProductRepo productRepo;
    @Resource
    private CompanyRepo companyRepo;
    @Resource
    private BlackRepo blackRepo;
    @Resource
    private CompanyManager companyManager;

    public ProductManager(ProductRepo productRepo, CompanyRepo companyRepo, BlackRepo blackRepo) {
        this.productRepo = productRepo;
        this.companyRepo = companyRepo;
        this.blackRepo = blackRepo;
    }

    /**
     * 按productId获取商品信息
     */

    public Product getProductInfoByProductId(String product) {
        for (Product productRepoProduct : productRepo.getProducts()) {
            if (productRepoProduct.getId().equals(product)) {
                return productRepoProduct;
            }
        }
        return null;
    }

    public boolean isBlackList(String productId) {
        Product product = getProductInfoByProductId(productId);
        CompanyVo company = companyManager.getParentCompany(product.getCompanyId());
        for (String blackProductList : blackRepo.getProductBlackList()) {
            if (blackProductList.equals(productId)) {
                return true;
            }
        }
        for (String blackCompanyList : blackRepo.getCompanyBlackList()) {
            if (blackCompanyList.equals(product.getCompanyId())) {
                return true;
            }
            if (company!= null) {
                if (blackCompanyList.equals(company.getId())) {
                    return true;
                }
            }


        }

        return false;
    }
}
